from lxml import html as etree


def get_logistics_total(html):
    dom = etree.fromstring(html)
    return int(dom.xpath('//input[@id="totalPageOrder"]/@value')[0])


def get_logistics_list(html):
    """
    获取物流追踪的每一条的集合
    :param html:
    :return:
    """

    dom = etree.fromstring(html)
    logistics_list = dom.xpath('//tbody[@id="dhSysMsg"]/tr[@class="content"]')
    print(f"{len(logistics_list)}")
    result = []
    for item in logistics_list:
        # 包裹号
        package_number = str(item.xpath('./@data-tracknum')[0])
        # 订单id
        order_id = str(item.xpath('@data-orderid')[0])
        # 详情跟踪号
        tracking_number = str(item.xpath('./td[5]/a/@data-trackingnumber')[0])
        # carrier_code
        carrier_code = str(item.xpath('./td[5]/a/@data-carriercode')[0])
        platform_name = str(item.xpath('./td[7]/text()')[0]).strip().split("：")[0]
        shop_name = str(item.xpath('./td[7]/text()')[1]).strip().split("：")[0]
        result.append({
            "package_number": package_number,
            "order_id": order_id,
            "tracking_number": tracking_number,
            "carrier_code": carrier_code,
            "platform_name": platform_name,
            "shop_name": shop_name
        })

    return result
